from sqlalchemy import Result, select
from fastapi import Depends, Form, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession

from . import crud, schemas
from core.models import User, db_helper
from core.config.log_config import MyLogger

main_logger = MyLogger()
logger = main_logger.getChild("user")  # type: ignore


async def get_user_by_phone_number(
    phone: str,
    session: AsyncSession = Depends(db_helper.get_async_session),
) -> User | None:

    user: User | None = await crud.get_user_by_phone(
        phone=phone,
        session=session,
    )

    return user


async def check_user_in_reference(user: User, session: AsyncSession = Depends(db_helper.get_async_session),) -> bool:

    item = await crud.check_user_in_refer(user=user, session=session)

    if item is None:
        return False
    else:
        return True


async def create_reference_for_user(
    data: schemas.TgChatReferSchema,
    user: User,
    session: AsyncSession = Depends(db_helper.get_async_session),
) -> None:

    await crud.create_chat_reference(
        chat_id=data.chat_id,
        tg_user_id=data.tg_user_id,
        user=user,
        session=session,
    )
